package e.odbo.data.generate.code;

import e.odbo.data.generate.GenerateConfig;
import e.odbo.data.generate.code.model.DataBaseMeta;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;

import java.io.File;
import java.io.StringWriter;

public class DataLogicHelperGenerator extends AbstractCodeClass {

    DataBaseMeta dataBaseMeta;

    public DataLogicHelperGenerator(DataBaseMeta dataBaseMeta, GenerateConfig config) {
        super(null, config);
        this.dataBaseMeta = dataBaseMeta;
    }

    @Override
    public String generateClass(Configuration cfg, String modelName) {
        StringWriter writer = null;
        try {
            cfg.setDirectoryForTemplateLoading(new File(getClass().getResource("/template/" + modelName).getFile()));
            //cfg.setObjectWrapper(new DefaultObjectWrapper());
            Template tpl = cfg.getTemplate("datalogichelper.ftl");
            writer = new StringWriter();
            tpl.process(dataBaseMeta, writer);
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }
        System.out.println("===文件 " + "DataLogicHelper.java" + " 生成成功===");
        return writer.toString();
    }
}
